<?php
/* @var $this Calendar_dayController */


$this->breadcrumbs=array(
	'Calendar Day',
);
date_default_timezone_set('Europe/Paris');
$connection = Yii::app()->db;
$date = isset($_GET['day']) ? $_GET['day'] : date('Y-m-d');
$previous_date = date('Y-m-d', strtotime($date .' -1 day'));
$next_date = date('Y-m-d', strtotime($date .' +1 day'));

$date_start = $date . " " . "00:00:00";
$date_end = $date . " " . "23:59:59";

//echo $this->userType;

if($this->userType == 2){
	
	$command=$connection->createCommand(
		"SELECT d.id as device_id,
			d.name as device_name,
			r.id as id,
			r.start_date as start_date,
			r.end_date as end_date,
			r.users_limit as users_limit
		FROM Reservations r
		JOIN Devices d ON r.Devices_id = d.id
		WHERE start_date >= '" . $date_start . "'
		AND end_date <= '" . $date_end . "'"
	);
	
	
	/*$command=$connection->createCommand(
		"SELECT r2.*, u.first_name, u.second_name, d.id as device_id, d.name as device_name FROM 
		Reservations r1 JOIN Reservations r2 ON r2.Reservations_id = r1.id 
		JOIN Users u ON u.id = r2.Users_id JOIN Devices d ON r2.Devices_id = d.id
		WHERE r1.start_date >= '" . $date_start . "' 
		AND r1.end_date <= '" . $date_end . "'"
	);*/
	
}else{

	$command=$connection->createCommand(
		"SELECT r2.*, u.first_name, u.second_name, d.id as device_id, d.name as device_name FROM 
		Reservations r1 JOIN Reservations r2 ON r2.Reservations_id = r1.id 
		JOIN Users u ON u.id = r2.Users_id JOIN Devices d ON r2.Devices_id = d.id
		WHERE r1.start_date >= '" . $date_start . "' 
		AND r1.end_date <= '" . $date_end . "'
		AND r1.type = 2 AND r2.type = 3"
	);

}

$dataReader=$command->query();
$devices = array();

foreach($dataReader as $row) {
	$devices[$row['device_id']][] = $row;
}

?>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<div class="switcher">
	<div class="actions">
		<div class="button">
			<input type="button" class="previous" value="Poprzedni" 
			onclick="window.location = '/pii/index.php/calendar_day?day=<?php echo $previous_date; ?>'" />
			
		</div>
		<div class="button name">
			<span><?php echo $date; ?></span>
		</div>
		<div class="button">
			<input type="button" class="next" value="Nastepny" 
			onclick="window.location = '/pii/index.php/calendar_day?day=<?php echo $next_date; ?>'" />
		</div>
	</div>
</div>
<div class="button">
	<input type="button" value="Dodaj" />
</div>
<div id="day-list">
	<div class="hours">
		<div class="hour" id="hour7">
			<span>7:00</span>
		</div>
		<div class="hour" id="hour8">
			<span>8:00</span>
		</div>
		<div class="hour" id="hour9">
			<span>9:00</span>
		</div>
		<div class="hour" id="hour10">
			<span>10:00</span>
		</div>
		<div class="hour" id="hour11">
			<span>11:00</span>
		</div>
		<div class="hour" id="hour12">
			<span>12:00</span>
		</div>
		<div class="hour" id="hour13">
			<span>13:00</span>
		</div>
		<div class="hour" id="hour14">
			<span>14:00</span>
		</div>
		<div class="hour" id="hour15">
			<span>15:00</span>
		</div>
		<div class="hour" id="hour16">
			<span>16:00</span>
		</div>
		<div class="hour" id="hour17">
			<span>17:00</span>
		</div>
		<div class="hour" id="hour18">
			<span>18:00</span>
		</div>
		<div class="hour" id="hour19">
			<span>19:00</span>
		</div>
		<div class="hour" id="hour20">
			<span>20:00</span>
		</div>
	</div>	
	<div class="stations">
		<?php 
		$drawing_column = 0;
		$column_count = count($devices);
		foreach ( $devices as $device ) {
			foreach( $device as $row ) {
				$id = "station_" . $row['id'];
				echo '<div class="station" id="'.$id.'">';
				echo '	<ul>';
				echo '		<li><strong>Nazwa:</strong> <span>'.$row['device_name'].'</span></li>';
				//echo '		<li><strong>Prowadzacy:</strong><span> '.$row['first_name'].' ' .$row['second_name']. '</span></li>';
				echo '		<li><strong>Uzytkownikow:</strong> <span>5/'.$row['users_limit'].'</span></li>';
				//echo '		<li><strong>Narzedzia:</strong> <span>???</span>';
				//echo '		<li><strong>start:</strong> <span>'.$row['start_date'].'</span>';
				//echo '		<li><strong>end:</strong> <span>'.$row['end_date'].'</span>';
				echo '		</li>';
				echo '	</ul>';
				echo '	<div class="actions">';
				echo '		<div class="button">';
				echo '			<input type="button" value="Edytuj" />';
				echo '		</div>';
				echo '		<div class="button">';
				echo '			<input type="button" value="Usun" />';
				echo '		</div>';
				echo '		<div class="button">';
				echo '			<input type="button" value="Konserwacja" />';
				echo '		</div>';
				echo '	</div>';
				echo '</div>';
				$parsed_start_date = date_parse ( $row['start_date']);
				$parsed_end_date = date_parse ( $row['end_date']);
				echo '<script>$(document).ready(function() {
						drawStation("#'.$id.'",'.$parsed_start_date['hour'].','.$parsed_start_date['minute'].','.$parsed_end_date['hour'].','.$parsed_end_date['minute'].','.$column_count.','.$drawing_column.');
					});</script>';
			}
			$drawing_column = $drawing_column + 1;
		}
		
		?>
	</div>
</div>
<script>
function drawStation(divId,startHour, startMinutes, stopHour, stopMinutes, columnCount, drawingColumn){
// godzina ma 60px wiec startMinutes stopMinutes mozna po prostu sumowac
var width=675;
var div=$(divId);
var top=$("#hour"+startHour).position().top;
var left=$("#hour"+startHour).position().left;
var bottom=$("#hour"+stopHour).position().top;
div.css("top",top+startMinutes);
div.css("left",left+50+(width/columnCount*drawingColumn));
div.css("right",left+50+(width/columnCount*(drawingColumn+1)));
div.css("bottom",-bottom-stopMinutes);
}

</script>